﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_MaintainPostings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        pn_Stff.Visible = false;
        if (!IsPostBack)
        {
            CompetitionDAO compeDAO = new CompetitionDAO();
            drd_Competitions.DataSource = compeDAO.GetAll();
            drd_Competitions.DataBind();
            loadExhibition();
            if(Session["selectCompetition"]!=null)
                    drd_Competitions.Items[int.Parse(Session["selectCompetition"].ToString())].Selected = true;                
            else
                drd_Competitions.Items[0].Selected=true;
            loadPostings();
        }

        if (Request["PostingID"] != null)
        {
            Control ct;
            ct = Page.LoadControl("~/Share/PostingDetailControl.ascx");
            pn_PostingDetail.Controls.Add(ct);
            pn_Stff.Visible = true;
        }
        SetRoleView();
        
        
    }
    public void loadPostings()
    {
        PostingDAO postDAO = new PostingDAO();
        int competitionID = int.Parse(drd_Competitions.SelectedValue.ToString());
        
        hplink_ViewDetailCompetition.NavigateUrl = "~/Admin/MaintainCompetitions.aspx?CompetitionID=" + drd_Competitions.SelectedValue.ToString();
        DataSet ds = postDAO.GetPostingInfoByCompetitionID(competitionID);
        if (ds.Tables.Count > 0)
        {
            grv_Postings.DataSource = ds;
            grv_Postings.DataBind();
            foreach (GridViewRow row in grv_Postings.Rows)
            {
                int id = int.Parse(grv_Postings.DataKeys[row.RowIndex].Value.ToString());
                ((Label)row.FindControl("lb_Mark")).Text = postDAO.Average(id); 
            }
        }
    }
    protected void drd_Competitions_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["selectCompetition"] = drd_Competitions.SelectedIndex;
        Response.Redirect("~/Admin/MaintainPostings.aspx");
    }

    protected void grv_Postings_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Postings.PageIndex = e.NewPageIndex;
        loadPostings();
    }

    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grv_Postings.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grv_Postings.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = false;
            }
        }
    }

    protected void grv_Postings_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }

    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        bool check = false;
        foreach (GridViewRow row in grv_Postings.Rows)
        {
            PostingDAO postingDAO = new PostingDAO();
            int postingID = int.Parse(grv_Postings.DataKeys[row.RowIndex].Value.ToString());
            check = ((CheckBox)row.FindControl("chk_Item")).Checked;
            if (check) 
            {
                if (postingDAO.DeletePosting(postingID) > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Successfull!');", true);
                }
                else
                {
                    string error = postingDAO.message;
                    if(error.StartsWith("The DELETE statement conflicted with the REFERENCE constraint \"FK_Sales_Postings\""))
                    {
                        string message = "alert('" + ((Label)row.FindControl("lb_postingname")).Text + " was sold! You must delete it in Maintain Sale before ');";
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", message, true);
                       // return;
                    }
                    else
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Fail!');", true);
                //    return;
                }
            }
            
        }
        if(check==false)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose a Posting (s) to delete!');", true);            
        loadPostings();        
    }
    protected void btnSendExhibition_Click(object sender, EventArgs e)
    {
        bool check = false;
        foreach (GridViewRow row in grv_Postings.Rows)
        {
            PostingExhibitionDAO postingExhibitionDAO = new PostingExhibitionDAO();
            PostingDAO postingDAO = new PostingDAO();
            int postingID = int.Parse(grv_Postings.DataKeys[row.RowIndex].Value.ToString());
            check = ((CheckBox)row.FindControl("chk_Item")).Checked;
            if (check)
            {                
                PostingExhibitionInfo eh = new PostingExhibitionInfo();
                eh.ExhibitionID = int.Parse(drdExhi.SelectedValue.ToString());
                eh.PostingID = postingID;
                eh.Status = "Selling";
                if (postingExhibitionDAO.Insert(eh)>0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Send to Exhibition Successfull!');", true);
                }
                else
                {
                    string message = "alert('Fail');";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", message, true);                    
                }
            }

        }
        if (check == false)
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose a Postings(s) to send!');", true);
        loadPostings();        
    }
    public void loadExhibition()
    {
        ExhibitionDAO ehDAO = new ExhibitionDAO();
        drdExhi.DataSource = ehDAO.GetUpcoming();
        drdExhi.DataBind();
    }
    protected void btnComment_Click(object sender, EventArgs e)
    {
        MarkDAO markDAO = new MarkDAO();
        MarkInfo mark = new MarkInfo();
        mark.Mark = drd_Mark.SelectedValue.ToString();
        mark.PostingId = int.Parse(Request["PostingID"]);
        mark.StaffId = Session["User"].ToString();
        mark.Remarks = txtCommnet.Text;
        if (markDAO.InsertMark(mark) > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Remarks Successful!')", true);
            pn_Stff.Visible = false;
        }

        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Fails');", true);
    }

    public void SetRoleView()
    {
        if (Session["roleID"] != null)
        {
            int roleID = int.Parse(Session["roleID"].ToString());
            if (roleID != 2)
            {
                pn_Stff.Visible = false;
            }
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ExhibitionDAO ehDAO = new ExhibitionDAO();
        
    }
}